import 'package:flutter/material.dart';
import 'package:v50art/AppStyle.dart';
import 'package:v50art/UserApi.dart';
import 'package:v50art/Util.dart';
import 'package:v50art/page/HomePage.dart';
import 'package:v50art/service/NetworkService.dart';
import 'package:v50art/widget/CommonButton.dart';
import 'package:v50art/widget/CommonLoading.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});
  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool showPhoneClear = false;
  var phoneController = TextEditingController();
  var passwordController = TextEditingController();
  bool hidePassword = true;
  @override
  void initState() {
    super.initState();
    phoneController.text = "17313219419";
    passwordController.text = "xieyao123";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            SizedBox(
              height: MediaQuery.of(context).size.height * 0.1,
            ),
            const Text(
              "欢迎登录",
              style: TextStyle(fontSize: 20),
            ),
            const Text(
              "V50ART",
              style: TextStyle(fontSize: 20),
            ),
            const SizedBox(
              height: 20,
            ),
            Container(
              margin: const EdgeInsets.all(10),
              padding: const EdgeInsets.all(20),
              decoration: BoxDecoration(
                  borderRadius: AppStyle.borderRadius,
                  color: Colors.white,
                  boxShadow: AppStyle.boxShadow),
              child: Column(
                children: [
                  TextField(
                    controller: phoneController,
                    onChanged: (s) {
                      setState(() {
                        showPhoneClear = s.isNotEmpty;
                      });
                    },
                    keyboardType: TextInputType.number,
                    inputFormatters: AppStyle.inputFormatterNumber,
                    decoration: InputDecoration(
                        prefixIcon: const Icon(Icons.phone_android),
                        hintText: "请输入手机号",
                        suffix: InkWell(
                            onTap: () {
                              if (showPhoneClear) {
                                phoneController.clear();
                                setState(() {
                                  showPhoneClear = false;
                                });
                              }
                            },
                            child: showPhoneClear
                                ? const Padding(
                                    padding: EdgeInsets.all(8.0),
                                    child: Icon(Icons.clear),
                                  )
                                : const SizedBox(
                                    width: 0,
                                    height: 0,
                                  ))),
                  ),
                  TextField(
                    inputFormatters: AppStyle.inputFormatterPassword,
                    controller: passwordController,
                    obscureText: hidePassword,
                    decoration: InputDecoration(
                        prefixIcon: const Icon(Icons.password),
                        hintText: "请输入密码",
                        suffix: InkWell(
                            onTap: () {
                              setState(() {
                                hidePassword = !hidePassword;
                              });
                            },
                            child: hidePassword
                                ? const Padding(
                                    padding: EdgeInsets.all(8.0),
                                    child: Icon(Icons.remove_red_eye_outlined),
                                  )
                                : const Padding(
                                    padding: EdgeInsets.all(8.0),
                                    child: Icon(Icons.remove_red_eye),
                                  ))),
                  ),
                ],
              ),
            ),
            Padding(
                padding: const EdgeInsets.only(top: 20),
                child: CommonButton(
                  title: "登录",
                  onTap: () async {
                    if (phoneController.text.isEmpty) {
                      Util.toast("请输入账号");
                      return;
                    }
                    if (passwordController.text.isEmpty) {
                      Util.toast("请输入密码");
                      return;
                    }
                    try {
                      CommonLoading.show();
                      var navigator = Navigator.of(context);
                      await UserApi.login(
                          phone: phoneController.text,
                          password: passwordController.text);
                      CommonLoading.hide();
                      navigator.pushReplacement(
                          MaterialPageRoute(builder: (c) => const HomePage()));
                    } catch (e) {
                      CommonLoading.hide();
                      if (e is RequestError) {
                        Util.toast(e.message);
                      }
                    }
                  },
                )),
          ],
        ),
      ),
    );
  }
}
